
import glob
import re
import string
import pepe.p_txt.bigtxt as bt
import pepe.p_filesystem.basic as pfb

fslist = lambda x, y: pfb.fslist(x, patterns=y, recurse=True, return_folders=False)

crlf = '\x0D\x0A'
files = fslist(  r"""\\dedus37\Cyrill_Rawdata\EIFFEL_Rev\2010-09-17\extracted\EMEA_FY99-FY02""" + '\x5C'
               , '*.txt')

o = open('sample.rows.dat', 'wb')
#o.write('...' + crlf)
#d = open('dropped.dat', 'wb')

filescnt = 0
for f in files:
    filescnt += 1
    #print f

    '''
    pf_masks = [
    r""" """,
    ]
    '''
    #for pf_mask in pf_masks:
    #print pf_mask
    #for inpfile in glob.glob('*.txt'):
    #for inpfile in glob.glob(pf_mask):

    fname = string.split(f, '\x5C')[-1]

    if fname not in ('FileList.txt', 'files.txt'):
        print f

        lino = 0
        for line in bt.lines(f):
            lino += 1

            line = re.sub(crlf, '', line)

            # take first N rows from each file
            if lino < 50:
                o.write(f + '\t' + line + crlf)
            '''
            if re.match('\d{7}', line[:7]) and len(line.split('	')) == 24:
                of.write(inpfile[:13]+'	'+line + crlf)
            else:
                trash_of.write(inpfile[:13]+'	'+line + crlf)
            '''
o.close()
#d.close()

print 'filescnt: ', filescnt
